package com.net.toooen.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.net.toooen.api.request.DownPhonenoRequest;
import com.net.toooen.api.request.LoginRequest;
import com.net.toooen.api.request.Request;
import com.net.toooen.api.request.UserInfoRequest;
import com.net.toooen.api.response.Response;
import com.net.toooen.entity.InterfaceKey;
import com.net.toooen.service.AppInterfaceService;

@Controller
@RequestMapping("/app")
public class AppInterfaceController extends BaseController {

	@Autowired
	protected AppInterfaceService appInterfaceService;

	private static final Logger logger = LoggerFactory
			.getLogger(AppInterfaceController.class);

	@RequestMapping(value = "/service.do")
	public void service(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Content-type", "application/json;charset=UTF-8");
		response.setCharacterEncoding("utf-8");

		String param = request.getParameter(InterfaceKey.text);

		long start = System.currentTimeMillis();
		logger.info("json request >>> " + param);

		Response resp = null;
		Gson gson = new Gson();
		try {
			Request req = gson.fromJson(param, Request.class);
			if (req != null) {
				switch (req.getMsgId().toUpperCase()) {
				// 登录
				case Request.LOGIN:
					resp = login(param);
					break;
				// 下载手机号
				case Request.DOWN_PHONENO:
					resp = downPhoneno(param);
					break;
				// 用户信息
				case Request.USER_RESOURCE:
					resp = userInfo(param);
					break;
				default:
					resp = Response.ERROR_NOT_METHOD;
					break;
				}
			} else {
				resp = Response.ERROR_PARAM;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resp = Response.ERROR_SERVER;
		}

		if (resp != null) {
			try {
				String result = gson.toJson(resp);
				response.getWriter().write(result);
				response.getWriter().close();

				long end = System.currentTimeMillis();
				logger.info("json response >>> 接口耗时：" + (end - start) + " >>> "
						+ result);
			} catch (IOException e) {
				logger.error(e.getMessage(), e);
			}
		}
	}


	/**
	 * @Description 用户信息
	 * @param param
	 * @return
	 */
	private Response userInfo(String param) {
		Gson gson = new Gson();
		UserInfoRequest req = gson.fromJson(param, UserInfoRequest.class);
		if (!isValid(req)) {
			return Response.ERROR_PARAM;
		}
		return appInterfaceService.userInfo(req);
	}


	/**
	 * @Title: login
	 * @Description: 登录
	 * @param
	 * @return
	 */
	private Response login(String param) {
		Gson gson = new Gson();
		LoginRequest req = gson.fromJson(param, LoginRequest.class);
		if (!isValid(req)) {
			return Response.ERROR_PARAM;
		}
		return appInterfaceService.login(req);
	}
	
	/**
	 *  下载手机号
	 * @return
	 */
	private Response downPhoneno(String param) {
		Gson gson = new Gson();
		DownPhonenoRequest req = gson.fromJson(param, DownPhonenoRequest.class);
		if (!isValid(req)) {
			return Response.ERROR_PARAM;
		}
		
		if(null == req.getPhoneNoCount()){
			req.setPhoneNoCount(5);
		}
		return appInterfaceService.downPhoneno(req);
	}


}
